package com.sc.nft.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.PledgeTaskReward;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.AdminPagePledgeRecordsRecordVO;
import com.sc.nft.entity.vo.PledgeSecretAreaPageVO;
import com.sc.nft.enums.PledgeStatusEnum;
import com.sc.nft.enums.RewardStatusEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.UserPledgeRecords;
import com.sc.nft.mapper.UserPledgeRecordsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 质押任务-用户质押记录(sc_user_pledge_records)数据DAO
 *
 * @author kancy
 * @since 2023-06-13 14:55:14
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class UserPledgeRecordsDao extends ServiceImpl<UserPledgeRecordsMapper, UserPledgeRecords> {

    public Page<AdminPagePledgeRecordsRecordVO> adminPagePledgeRecordsRecord(Long pledgeTaskId,String userTel, Integer pageNo, Integer pageSize) {
        return baseMapper.adminPagePledgeRecordsRecord(new Page<>(pageNo, pageSize),pledgeTaskId,userTel);
    }

    public Integer getCountByUserIdAndPledgeTaskIdAndStatus(Long userId, Long pledgeTaskId, PledgeStatusEnum pledgeStatus) {
        LambdaQueryWrapper<UserPledgeRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPledgeRecords::getUserId, userId);
        wrapper.eq(UserPledgeRecords::getPledgeTaskId, pledgeTaskId);
        wrapper.eq(ObjectUtil.isNotNull(pledgeStatus),UserPledgeRecords::getPledgeStatus, pledgeStatus);
        return count(wrapper);
    }

    public Page<PledgeSecretAreaPageVO> pledgeSecretAreaPage(UserInfo user,Long pledgeTaskId, Integer pageNo, Integer pageSize) {
        return baseMapper.pledgeSecretAreaPage(new Page<>(pageNo, pageSize),user.getId(),pledgeTaskId);
    }

    public UserPledgeRecords getOneByUserIdAndId(Long userPledgeRecordsId, Long userId) {
        LambdaQueryWrapper<UserPledgeRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPledgeRecords::getUserId, userId);
        wrapper.eq(UserPledgeRecords::getId, userPledgeRecordsId);
        return getOne(wrapper);
    }

    public List<UserPledgeRecords> getListOneMinuteLater() {
        return baseMapper.getListOneMinuteLater();
    }

    public Boolean updateRewardStatusById(Long id, RewardStatusEnum rewardStatus) {
        UpdateWrapper<UserPledgeRecords> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("reward_status", rewardStatus.getCode());
        return update(new UserPledgeRecords(),wrapper);
    }

    public Boolean updatePledgeStatusAndRewardStatusById(Long id, PledgeStatusEnum nowStatus, PledgeStatusEnum pledgeStatus, RewardStatusEnum rewardStatus) {
        UpdateWrapper<UserPledgeRecords> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq("pledge_status", nowStatus.getCode());
        wrapper.set("reward_status", rewardStatus.getCode());
        wrapper.set("pledge_status", pledgeStatus.getCode());
        return update(new UserPledgeRecords(),wrapper);
    }

    public Integer getCountByUserIdAndPledgeTaskIdAndCollectionIdAndStatus(Long userId, Long pledgeTaskId, Long pledgeTaskCollectionId, PledgeStatusEnum pledgeStatus) {
        LambdaQueryWrapper<UserPledgeRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPledgeRecords::getUserId, userId);
        wrapper.eq(UserPledgeRecords::getPledgeTaskId, pledgeTaskId);
        wrapper.eq(UserPledgeRecords::getPledgeTaskCollectionId, pledgeTaskCollectionId);
        wrapper.eq(ObjectUtil.isNotNull(pledgeStatus),UserPledgeRecords::getPledgeStatus, pledgeStatus);
        return count(wrapper);
    }

    public Integer getCountByUserIdAndPledgeTaskIdAndPledgeTaskCollectionIdAndStatus(Long userId, Long pledgeTaskId, Long pledgeTaskCollectionId, PledgeStatusEnum pledgeStatus) {
        LambdaQueryWrapper<UserPledgeRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPledgeRecords::getUserId, userId);
        wrapper.eq(UserPledgeRecords::getPledgeTaskId, pledgeTaskId);
        wrapper.eq(UserPledgeRecords::getPledgeTaskCollectionId, pledgeTaskCollectionId);
        wrapper.eq(ObjectUtil.isNotNull(pledgeStatus),UserPledgeRecords::getPledgeStatus, pledgeStatus);
        return count(wrapper);
    }

    public Boolean updateSuccessForPledgeStatusById(Long id) {
        UpdateWrapper<UserPledgeRecords> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq("pledge_status", PledgeStatusEnum.END_OF_PLEDGE.getCode());
        wrapper.eq("reward_status", RewardStatusEnum.DISTRIBUTING.getCode());
        wrapper.set("reward_status", RewardStatusEnum.ISSUED);
        return update(new UserPledgeRecords(),wrapper);
    }
}